package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppDynamic;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 动态
 */
@Repository
public class AppDynamicDao extends BaseDao<AppDynamic> {

    /**
     * 查询指定用户动态列表
     */
    public List<AppDynamic> specificUserList(Integer userid, SplitPageBean splitPage) {
        if (userid == null || userid <= 0) {
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic where status = 1  and userid = ?  ");
        paramList.add(userid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppDynamic.class, splitPage);
    }


    /**
     * 查询指定用户动态列表
     */
    public List<AppDynamic> selectPage(Map<String, Object> paramap, SplitPageBean splitPage) {
        ArrayList<Object> list = new ArrayList<Object>();
        StringBuilder str = new StringBuilder("select * from app_dynamic where status = 1");

        if (StringEx.isNotNull(paramap.get("id"))) {
            str.append(" and id = ? ");
            list.add(paramap.get("id"));
        }

        if (StringEx.isNotNull(paramap.get("userIds"))) {
            str.append(" and userId in ( ");
            str.append(paramap.get("userIds"));
            str.append(" ) ");
        }

        //过滤
        if (paramap.containsKey("states") && StringEx.isNotNull(paramap.get("states"))) {
            str.append(" and userid not in (  ");
            str.append(paramap.get("states"));
            str.append(" )  ");
        }
        str.append(" order by createtime desc");
        String sql = str.toString();
        return this.queryList(sql, list, AppDynamic.class, splitPage);
    }

    /**
     * 查询指定用户动态列表
     */
    public List<AppDynamic> selectNearbyPage(Map<String, Object> params, SplitPageBean splitPage) {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("select *,acos(sin((?*3.1415)/180) * sin((lat*3.1415)/180) + " +
                "                             cos((?*3.1415)/180) * cos((lat*3.1415)/180) * cos((?*3.1415)/180 - (lon*3.1415)/180) " +
                "                             )*6370.996 as distince from app_dynamic " +
                "                 where status = 1 ");
        if (params.containsKey("hiddenUserIds")) {
            str.append(" and userid not in (" + params.get("hiddenUserIds") + ")");
        }
        str.append(" having distince <= 50 order by createtime desc, distince asc");
        list.add(params.get("lat"));
        list.add(params.get("lat"));
        list.add(params.get("lon"));
        String sql = str.toString();
        return this.queryList(sql, list, AppDynamic.class, splitPage);
    }

    /**
     * 详情
     */
    public AppDynamic get(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic where id = ?  ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppDynamic.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppDynamic haoYou) {
        if (haoYou != null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id == null || id <= 0) {
            return;
        }
        String sql = "delete  from app_dynamic where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete  from app_dynamic where userid = " + userId;
        this.del(sql, new ArrayList<>());
    }

    //更新状态
    public void updateStatus(Integer id, Integer status) {
        if (id == null || id <= 0 || status == null || status <= 0) {
            return;
        }
        String sql = "update app_dynamic set status = ? where id = ?";
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(status);
        paramList.add(id);
        this.update(sql, paramList);
    }


    /**
     * 更新用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (userid == null || userid <= 0 || isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_dynamic SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateNick(Integer userid, String nick) {
        if (userid == null || userid <= 0 || isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_dynamic SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户性别
     *
     * @param userid 1
     * @param sex    性别
     */
    public void updateSex(Integer userid, Integer sex) {
        if (userid == null || userid <= 0 || isNull(sex)) {
            return;
        }
        String sql = " UPDATE app_dynamic SET sex = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql, paramList);
    }


    /**
     * 动态点赞数量变更
     *
     * @param id     动态id
     * @param status 操作状态  1增加  2减少
     */
    public void dianzanNumUpdate(Integer id, Integer status) {
        if (id == null || id <= 0 || status == null || status <= 0) {
            return;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer("update app_dynamic set");
        if (status == 1) {
            sql.append(" givenum = givenum+1 ");
        } else if (status == 2) {
            sql.append(" givenum = givenum-1 ");
        }
        sql.append(" where id = ?");
        paramList.add(id);
        this.update(sql.toString(), paramList);
    }

    /**
     * 动态评论数量变更
     *
     * @param id     动态id
     * @param status 操作状态  1增加  2减少
     */
    public void pinglunNumUpdate(Integer id, Integer status) {
        if (id == null || id <= 0 || status == null || status <= 0) {
            return;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer("update app_dynamic set");
        if (status == 1) {
            sql.append(" commentnum = commentnum+1 ");
        } else if (status == 2) {
            sql.append(" commentnum = commentnum-1 ");
        }
        sql.append(" where id = ?");
        paramList.add(id);
        this.update(sql.toString(), paramList);
    }

    /**
     * 删除动态
     *
     * @Param: [id]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/29
     * @Time: 11:47
     */
    public void delDongtai(Integer id) {
        if (id == null || id <= 0) {
            return;
        }
        String sql = " UPDATE app_dynamic SET status = 2  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 转发数量变更
     *
     * @Param: [id]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/29
     * @Time: 11:54
     */
    public void zhuanfaUpdate(Integer id) {
        if (id == null || id <= 0) {
            return;
        }
        String sql = " UPDATE app_dynamic SET zhuanfa = (CASE WHEN zhuanfa IS NULL THEN 1 ELSE zhuanfa +1 END) WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.update(sql, paramList);
    }


    /**
     * app查询列表
     *
     * @param arr 屏蔽的用户
     */
    public List<AppDynamic> applist(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic where 1=1 and status = 1 ");
        if (paraMap.containsKey("excludeUserId") && StringEx.isNotNull(paraMap.get("excludeUserId"))) {
            sbStr.append(" and userid not in (  ");
            sbStr.append(paraMap.get("excludeUserId"));
            sbStr.append(" )  ");
        }
        if (paraMap.containsKey("sex") && StringEx.isNotNull(paraMap.get("sex"))) {
            sbStr.append(" and (sex = " + paraMap.get("sex") + " or userid =" + paraMap.get("userId") + " ) ");
        }
        if (paraMap.containsKey("states") && StringEx.isNotNull(paraMap.get("states"))) {
            sbStr.append(" and userid not in (  ");
            sbStr.append(paraMap.get("states"));
            sbStr.append(" )  ");
        }
        if (paraMap.containsKey("ids") && StringEx.isNotNull(paraMap.get("ids"))) {
            sbStr.append(" and id in (  ");
            sbStr.append(paraMap.get("ids"));
            sbStr.append(" ) ");
        }
        sbStr.append(" and status = 1 ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppDynamic.class, splitPage);
    }

    public List<AppDynamic> list(Map<String, Object> params) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic where 1=1 and status = 1 ");

        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppDynamic.class);
    }

    public List<AppDynamic> myDynamic(Integer userId) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_dynamic where status = 1  and userid = ?  ");
        paramList.add(userId);
        sbStr.append(" order by createtime desc");
        return this.queryList(sbStr.toString(), paramList, AppDynamic.class);
    }

}
